package com.project.weimi.adapter;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.activity.MyCollectionActivity;
import com.project.weimi.entity.response.CollectionInvitationEntity;
import com.project.weimi.interfaces.ItemClickListener;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.TextView;

public class CollectionInvitationAdapter extends RecyclerView.Adapter<ViewHolder>{

	private List<CollectionInvitationEntity> list;
	private Context context;
	private ItemClickListener itemClickListener;
	
	private static final int TYPE_ITEM = 0;
	private static final int TYPE_FOOTER = 1;
	
	public List<CollectionInvitationEntity> getList() {
		return list;
	}

	public CollectionInvitationAdapter(Context context) {
		list = new ArrayList<CollectionInvitationEntity>();
		this.context = context;
	}

	public void setItemClickListener(ItemClickListener itemClickListener){
		this.itemClickListener = itemClickListener;
	}
	
	@Override
	public int getItemCount() {
		return list.size() > 5 ? list.size() + 1 : list.size();
	}

	@Override
	public int getItemViewType(int position) {
		if(position + 1 == getItemCount() && getItemCount() > 5){
			return TYPE_FOOTER;
		}else {
			return TYPE_ITEM;
		}
	}

	@Override
	public void onBindViewHolder(final ViewHolder arg0, final int arg1) {
		if(arg0 instanceof ItemViewHolder){
			
			((ItemViewHolder)arg0).checkbox.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					list.get(arg1).setCheckbox(((ItemViewHolder)arg0).checkbox.isChecked());
					((MyCollectionActivity)context).getInvitationFrag().isAllCheck();
				}
			});
			
			
			((ItemViewHolder)arg0).checkbox.setChecked(list.get(arg1).isCheckbox());
			((ItemViewHolder)arg0).tv_content.setText(list.get(arg1).getContent());
			((ItemViewHolder)arg0).tv_tip.setText(list.get(arg1).getTip());
			((ItemViewHolder)arg0).tv_num.setText(list.get(arg1).getNum());
			
		}
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		if(arg1 == TYPE_ITEM){
			View view = LayoutInflater.from(context).inflate(R.layout.collection_invitation_recyclerview_item, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new ItemViewHolder(view);
		}else if(arg1 == TYPE_FOOTER){
			View view = LayoutInflater.from(context).inflate(R.layout.shopping_footerview, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new FooterViewHolder(view);
		}
		
		return null;
	}

	class FooterViewHolder extends ViewHolder{

		public FooterViewHolder(View itemView) {
			super(itemView);
		}
	}
	
	class ItemViewHolder extends ViewHolder{

		CheckBox checkbox;
		TextView tv_content;
		TextView tv_tip;
		TextView tv_num;
		
		public ItemViewHolder(View itemView) {
			super(itemView);
			checkbox = (CheckBox)itemView.findViewById(R.id.checkbox);
			tv_content = (TextView)itemView.findViewById(R.id.tv_content);
			tv_tip = (TextView)itemView.findViewById(R.id.tv_tip);
			tv_num = (TextView)itemView.findViewById(R.id.tv_num);
		}
	}
	
	
}
